<template>
  <div class="account-settings-info-view">
    <a-form :form="form" style="margin:30px 10px 10px 10px;flex: 1;">

      <a-form-item
              label="是否开启分销功能"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
      >
        <r-switch :formData="formData" field="open_seller_reward" :checkedValue="1" :unCheckedValue="2" checkedChildren="开启" unCheckedChildren="关闭" />
      </a-form-item>


      <a-form-item
              v-show="formData.open_seller_reward==1"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              label="奖励模式"
              hasFeedback
      >
        <a-radio-group v-model="formData.seller_reward_mode" buttonStyle="solid">
          <a-radio-button :value="1">统一设置</a-radio-button>
          <a-radio-button :value="2">按用户组设置</a-radio-button>
        </a-radio-group>

      </a-form-item>


      <a-form-item
              v-show="formData.open_seller_reward==1&& formData.seller_reward_mode==1"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              label="品荐师提成"
              hasFeedback
              extra="品荐师提成=完成订单实际支付商品金额的千分之几"
      >
        <a-input type="number" v-decorator="[
          'seller_reward_percent',
          {rules: [{ required: true, message: '请输入比例' }]}
        ]"
                 placeholder="请输入比例"/>
      </a-form-item>
      <a-form-item
              v-show="formData.open_seller_reward==1&& formData.seller_reward_mode==1"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              label="团长提成"
              hasFeedback
              extra="团长提成=完成订单实际支付商品金额的千分之几"
      >
        <a-input type="number" v-decorator="[
          'team_reward_percent',
          {rules: [{ required: true, message: '请输入比例' }]}
        ]"
                 placeholder="请输入比例"/>
      </a-form-item>

      <a-table
              v-show="formData.open_seller_reward==1&& formData.seller_reward_mode==2"
              :columns="sellerGroupColumns" :dataSource="formData.sellerGroupRewardList" :locale="{emptyText:'请先配置销售用户组'}" :pagination="false" rowKey="id"  bordered>
        <template slot="sellerPercent" slot-scope="text,record,index">
          <a-input type="number"  v-model="record.seller_percent" placeholder="千分之几"/>
        </template>
        <template slot="teamPercent" slot-scope="text,record,index">
          <a-input type="number" v-model="record.team_percent" placeholder="千分之几"/>
        </template>
        <template slot="sellerPercentTitle">
          销售提成比例
          <a-popover content="请设置千分之几">
            <a-icon type="question-circle" />
          </a-popover>
        </template>
        <template slot="teamPercentTitle">
          团长提成比例
          <a-popover content="请设置千分之几">
            <a-icon type="question-circle" />
          </a-popover>
        </template>
      </a-table>


      <a-affix :offsetBottom="0">
        <div style="background: #fff; padding: 10px 0 20px 0 ; border-top: 1px solid #eee  ">
          <a-button type="primary" style="margin-right: 10px;" :loading="saveLoading" @click="submitForm">保存</a-button>
        </div>
      </a-affix>
    </a-form>

  </div>
</template>

<script>
  import { sellerRewardSetting,getSellerRewardSetting } from '@/api/business'
  import RSwitch from '@/components/RSwitch/'
  export default {
    components: {
      RSwitch
    },
    data () {
      return {
        saveLoading:false,
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        form: this.$form.createForm(this),

        //人员管理表头
        sellerGroupColumns: [
          {
            title: '用户组',
            width:'100px',
            dataIndex: 'name',
          },
          {
            width:'100px',
            dataIndex: 'seller_percent',
            slots: { title: 'sellerPercentTitle' },
            scopedSlots: { customRender: 'sellerPercent' },
          },
          {
            width:'100px',
            dataIndex: 'team_percent',
            slots: { title: 'teamPercentTitle' },
            scopedSlots: { customRender: 'teamPercent' },
          },
        ],
        sellerGroupRewardList:[
        ],

        formData:{
          open_seller_reward:2,
          seller_reward_mode:1,
          sellerGroupRewardList:[]
        },

      }
    },
    mounted(){
      let that = this;
      that.fetch();
    },
    methods: {
      fetch(){
        let that = this;
        getSellerRewardSetting({
        }) .then(res => {
          that.formData = res.data;
          that.form.setFieldsValue({...res.data});
        }).catch(err =>{
        })

      },
      submitForm(e){
        let self = this
        e.preventDefault()
        this.form.validateFields((err, values) => {
          if (!err) {
            self.saveLoading = true;
            console.log('Received values of form: ', values)
            console.log('Received valuess of formData: ', self.formData)
            sellerRewardSetting(Object.assign(self.formData,values)).then(res => {
              self.$message.success('设置成功')
              self.saveLoading = false;
            }).catch(err =>{
              self.$message.error(err.data.msg);
              self.saveLoading = false;
            })
          }else{
            console.log(err);
          }
        })
      },
    }
  }
</script>

<style lang="less" scoped>

  .avatar-upload-wrapper {
    height: 200px;
    width: 100%;
  }

  .ant-upload-preview {
    position: relative;
    margin: 0 auto;
    width: 100%;
    max-width: 180px;
    border-radius: 50%;
    box-shadow: 0 0 4px #ccc;

    .upload-icon {
      position: absolute;
      top: 0;
      right: 10px;
      font-size: 1.4rem;
      padding: 0.5rem;
      background: rgba(222, 221, 221, 0.7);
      border-radius: 50%;
      border: 1px solid rgba(0, 0, 0, 0.2);
    }
    .mask {
      opacity: 0;
      position: absolute;
      background: rgba(0,0,0,0.4);
      cursor: pointer;
      transition: opacity 0.4s;

      &:hover {
        opacity: 1;
      }

      i {
        font-size: 2rem;
        position: absolute;
        top: 50%;
        left: 50%;
        margin-left: -1rem;
        margin-top: -1rem;
        color: #d6d6d6;
      }
    }

    img, .mask {
      width: 100%;
      max-width: 180px;
      height: 100%;
      border-radius: 50%;
      overflow: hidden;
    }
  }
</style>